package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.page.PageBean;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.vo.GoodsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@DubboService
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private TbGoodsMapper tg;

    @Resource
    private TbGoodsDescMapper td;

    @Resource
    private TbItemMapper ti;

    @Resource
    private TbItemCatMapper tcm;

    @Override
    public List<GoodsVO> getAll(String goodsName) {
        //查出全部
        List<TbGoods> goodsList = tg.getGoodsList(goodsName);
        List<GoodsVO> list = new ArrayList();
        for (TbGoods goods : goodsList) {
            GoodsVO vo = new GoodsVO();
            //商品Id
            vo.setId(goods.getId());
            //商品名称
            vo.setGoodsName(goods.getGoodsName());
            //商品价格
            vo.setPrice(goods.getPrice());
            //商品状态
            if (goods.getAuditStatus().equals("0")){
                vo.setAuditStatus("审核中");
            }
            if (goods.getAuditStatus().equals("1")){
                vo.setAuditStatus("审核通过");
            }
            if (goods.getAuditStatus().equals("2")){
                vo.setAuditStatus("审核不通过");
            }
            //一级分类
            if (goods.getCategory1Id() == null){
                vo.setName1(null);
            }else {
                TbItemCat c1 = tcm.selectByPrimaryKey(goods.getCategory1Id());
                vo.setName1(c1.getName());
            }
            //二级分类
            if (goods.getCategory2Id() == null){
                vo.setName2(null);
            }else {
                TbItemCat c2 = tcm.selectByPrimaryKey(goods.getCategory2Id());
                vo.setName2(c2.getName());
            }
            //三级分类
            if (goods.getCategory3Id() == null){
                vo.setName3(null);
            }else {
                TbItemCat c3 = tcm.selectByPrimaryKey(goods.getCategory3Id());
                vo.setName3(c3.getName());
            }
            list.add(vo);
        }
        return list;
    }

    //删除
    @Override
    public void deleteCheck(Long[] ids) {
        tg.deleteCheck(ids);
    }

    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return tg.selectByPrimaryKey(goodsId);
    }

    //审核
    public void update(String auditStatus,String[] ids){
        for (String id : ids) {
            tg.updateUp(id,auditStatus);
        }
    }

    //增加
    @Transactional
    @Override
    public void addGoodsAndDese(TbGoods goods, TbGoodsDesc desc, List<TbItem> itemList) {
        goods.setAuditStatus("0");
        tg.insertSelective(goods);
        desc.setGoodsId(goods.getId());
        td.insertSelective(desc);
        Long id = goods.getId();
        for (TbItem item : itemList) {
            item.setGoodsId(id);
            ti.insertSelective(item);
        }
    }
}
